package com.motorola.examples;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.ContextExecutorService;
import com.WASThreadFactory;
import com.ibm.websphere.asynchbeans.Work;
import com.ibm.websphere.asynchbeans.WorkManager;

public class GenericExecutor {
	
	int numRunnables = 10;
	int queueSize = 100000;
	int corePoolSize = 1;
	int maxPoolSize = 10;
	long keepAliveTime = 2000;
	int numIterations = 10;
	int delayBtwnIterations = 1;
	boolean useContextExecutor = false;
	String wmJNDIName = "java:comp/env/wm/DefaultWorkManager";
	WorkManager wm = null;
	InitialContext ctx;
	BlockingQueue<Runnable> queue;
	ThreadFactory threadFactory;
	ThreadPoolExecutor pool;
	ExecutorService executorService; 
	
	public GenericExecutor(){
	
		try {
			ctx = new InitialContext();
			wm = (WorkManager) ctx.lookup(wmJNDIName);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		queue = new ArrayBlockingQueue<Runnable>(queueSize);
		
		threadFactory = new WASThreadFactory(wm);

		pool = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime,
				TimeUnit.MILLISECONDS, queue, threadFactory);
		executorService = new ContextExecutorService(wm, pool);
	}
	
	public void execute(Work work){
	
		executorService.execute(work);
	}
	
}
